package com.hc.acoustics.ui.activity

import android.content.Intent
import android.os.Bundle
import com.hc.acoustics.R
import com.hc.acoustics.base.BaseActivity
import com.hc.acoustics.util.FormulaUtil
import com.hc.acoustics.util.TextChangeCall
import com.hc.acoustics.util.UiHelperUtil
import com.hc.acoustics.widget.CommonPopupWindow
import com.yc.yclibrary.base.YcAppCompatActivity
import com.yc.yclibx.adapter.YcAdapterHelper
import com.yc.yclibx.comment.YcEmpty
import kotlinx.android.synthetic.main.actionbar_layout.*
import kotlinx.android.synthetic.main.type2_formula1_location_activity.*
import kotlinx.android.synthetic.main.formula_button.*
import kotlinx.android.synthetic.main.type3_formula2_location_activity.*

/**
 *  声学 - 频率->波长->周期
 */
class Type2Formula1Activity : BaseActivity() {
    lateinit var mPopupwindow: CommonPopupWindow<String>
    var typeFormula: Int = 0

    companion object {
        private const val KEY_TITLE = "key_title"
        @JvmStatic
        fun newInstance(activity: YcAppCompatActivity, title: String) {
            val intent = Intent(activity, Type2Formula1Activity::class.java)
            intent.putExtra(KEY_TITLE, title)
            activity.startActivity(intent)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.type2_formula1_location_activity
    }

    override fun initView(savedInstanceState: Bundle?) {
        setActionBar(intent.getStringExtra(KEY_TITLE))
        formulaResetBtn.setOnClickListener {
            formula1_c_TEV.reset()
            formula1_v_TEV.reset()
            formula1_f_TEV.reset()
            formula1_b_TEV.reset()
            formula1_t_TEV.reset()
        }
        formulaOkBtn.setOnClickListener {
            try {
                val resultV = FormulaUtil.formula1_1(formula1_c_TEV.getDouble())
                formula1_v_TEV.setText(resultV)
                when (typeFormula) {
                    0 -> {
                        try {
                            val resultB =
                                FormulaUtil.formula1_b(resultV, formula1_f_TEV.getDouble())
                            val resultT = FormulaUtil.formula1_t(formula1_f_TEV.getDouble())
                            formula1_b_TEV.setText(resultB)
                            formula1_t_TEV.setText(resultT)
                        } catch (e: NumberFormatException) {
                            formula1_b_TEV.setText("Infinity")
                            formula1_t_TEV.setText("Infinity")
                        }
                    }
                    1 -> {
                        try {
                            val resultF =
                                FormulaUtil.formula1_f(resultV, formula1_b_TEV.getDouble())
                            val resultT = FormulaUtil.formula1_t(resultF)
                            formula1_f_TEV.setText(resultF)
                            formula1_t_TEV.setText(resultT)
                        } catch (e: NumberFormatException) {
                            formula1_f_TEV.setText("Infinity")
                            formula1_t_TEV.setText("Infinity")
                        }
                    }
                    2 -> {
                        try {
                            val resultF = FormulaUtil.formula1_f(formula1_t_TEV.getDouble())
                            val resultB = FormulaUtil.formula1_b(resultV, resultF)
                            formula1_f_TEV.setText(resultF)
                            formula1_b_TEV.setText(resultB)
                        } catch (e: NumberFormatException) {
                            formula1_f_TEV.setText("Infinity")
                            formula1_b_TEV.setText("Infinity")
                        }
                    }
                }
            } catch (e: NumberFormatException) {
                formula1_v_TEV.setText("Infinity")
            }
        }
        mPopupwindow = object : CommonPopupWindow<String>(this) {
            override fun onUpdate(helper: YcAdapterHelper, item: String?, position: Int) {
                helper.setText(R.id.weightPopupWindowDetailsItemTv, item)
            }
        }
        mPopupwindow.selectItem { viewHolder, view, position ->
            typeFormula = position
            formula1_f_TEV.reset()
            formula1_b_TEV.reset()
            formula1_t_TEV.reset()
            when (position) {
                0 -> {
                    formula1TypeTv.setText("求波长和周期")
                    formula1_f_TEV.setEdtType(true)
                    formula1_b_TEV.setEdtType(false)
                    formula1_t_TEV.setEdtType(false)
                }
                1 -> {
                    formula1TypeTv.setText("求频率和周期")
                    formula1_f_TEV.setEdtType(false)
                    formula1_b_TEV.setEdtType(true)
                    formula1_t_TEV.setEdtType(false)
                }
                2 -> {
                    formula1TypeTv.setText("求频率和波长")
                    formula1_f_TEV.setEdtType(false)
                    formula1_b_TEV.setEdtType(false)
                    formula1_t_TEV.setEdtType(true)
                }
            }
            mPopupwindow.dismiss()
        }
        mPopupwindow.addData(mutableListOf("求波长和周期", "求频率和周期", "求频率和波长"))
        formula1TypeTv.setOnClickListener {
            mPopupwindow.showAsDropDown(it)
        }
    }
}